package csc420groupproject;

import java.awt.Component;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author drenz
 */
public class CustomChangeListener implements ChangeListener{
    private boolean isDebug = false;
    private GUI gui;

    /**
     * 
     * @param isDebug
     */
    public CustomChangeListener(boolean isDebug){
        super();
        this.isDebug = isDebug;
    }

    /**
     * 
     */
    public CustomChangeListener(){
        super();
    }

    /**
     * 
     * @param e
     */
    public void stateChanged(ChangeEvent e) {
        if(e.getSource() instanceof JTabbedPane){
            JTabbedPane tabbedPane = (JTabbedPane)e.getSource();
            this.gui = (GUI)tabbedPane.getTopLevelAncestor();

            if(tabbedPane.getSelectedComponent() instanceof JToolBar)
                try{
                    if(tabbedPane.getSelectedComponent().getName().equals("mathToolBar")){
                        
                        if(isDebug)
                            System.out.println("MATH tab selected");

                        this.gui.formulaMainPanel.setVisible(true);
                        this.alternateModes(true);
                    } else if(tabbedPane.getSelectedComponent().getName().equals("textToolBar")){
                        
                        if(isDebug)
                            System.out.println("TEXT tab selected");

                        this.gui.formulaMainPanel.setVisible(false);
                        this.alternateModes(false);
                    }
                }catch (NullPointerException err){
                    System.out.println("Component inside of JTabbedPane doesn't have"
                            + " a name to compare.");
                    err.printStackTrace();
                }
        }
    }

     private void alternateModes(boolean isMathModeEnabling) {
        JMenuBar menuBar = this.gui.getJMenuBar();

        for (Component n : menuBar.getComponents()) {
            //we check this just in case there is a non JMenu in the JMenuBar
            if (n instanceof JMenu) {
                JMenu castedN = (JMenu) n;

                if (castedN.getText().equals("View")) {

                    for (Component j : castedN.getMenuComponents()) {
                        if (j instanceof JMenuItem) {
                            JMenuItem castedJ = (JMenuItem) j;

                            if (castedJ.getText().equals("Text Mode")) {

                                if (this.isDebug) {
                                    System.out.println(isMathModeEnabling + " text mode button");
                                }

                                castedJ.setEnabled(isMathModeEnabling);
                            } else if (castedJ.getText().equals("Math Mode")) {

                                if (this.isDebug) {
                                    System.out.println(!isMathModeEnabling + " math mode button");
                                }

                                castedJ.setEnabled(!isMathModeEnabling);
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
}
